package lib_model.Order;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;

import lib_dal.Connection.ConnectMSSQL;
import lib_dal.Interface.ISql;
import lib_model.Orther.ReturnObj;

public class Orders_bill implements ISql {

	public int id;
	public int warehouse_id_ref;// cua hang
	public String orders_bill_type_name_ref;// tinh trang
	public String account_warehouse_username_ref;// nguoi tiep nhan
	public int customer_id_ref;// nguoi mua
	public Date date_create;

	public Orders_bill() {
	}

	@Override
	public ReturnObj Select() {
		String sql = "select id, warehouse_id_ref, orders_bill_type_name_ref, account_warehouse_username_ref, customer_id_ref, date_create from dbo.orders_bill";
		ReturnObj obj = new ReturnObj();
		ArrayList<Orders_bill> arr = new ArrayList<Orders_bill>();
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			ResultSet rs = preState.executeQuery();
			while (rs.next()) {
				Orders_bill a = new Orders_bill();
				a.id = rs.getInt("id");
				a.warehouse_id_ref = rs.getInt("warehouse_id_ref");
				a.orders_bill_type_name_ref = rs
						.getString("orders_bill_type_name_ref");
				a.account_warehouse_username_ref = rs
						.getString("account_warehouse_username_ref");
				a.customer_id_ref = rs.getInt("customer_id_ref");
				a.date_create = rs.getDate("date_create");
				arr.add(a);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		obj.ObjResult = arr;
		return obj;
	}

	@Override
	public ReturnObj Add() {
		String sql = "insert into dbo.orders_bill (warehouse_id_ref,   orders_bill_type_name_ref,   account_warehouse_username_ref,   customer_id_ref,   date_create) values   (?,   ?,  ?,   ?,   GETDATE())";
		ReturnObj obj = new ReturnObj();
		int key = 0;
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			preState.setInt(1, this.warehouse_id_ref);
			preState.setString(2, this.orders_bill_type_name_ref);
			preState.setString(3, this.account_warehouse_username_ref);
			preState.setInt(4, this.customer_id_ref);
			obj.Result = (preState.executeUpdate() == 1)? true:false;
			ResultSet keys = preState.getGeneratedKeys();  
			keys.next(); 
			key = keys.getInt(1); 
			keys.close();
			preState.close();
			obj.ObjResult = key;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return obj;
	}

	@Override
	public ReturnObj Update() {
		String sql = "update dbo.orders_bill set  warehouse_id_ref = ?,   orders_bill_type_name_ref = ?,   account_warehouse_username_ref = ?,   customer_id_ref = ?,   date_create = ? where  id = ?";
		ReturnObj obj = new ReturnObj();
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			preState.setInt(1, this.warehouse_id_ref);
			preState.setString(2, this.orders_bill_type_name_ref);
			preState.setString(3, this.account_warehouse_username_ref);
			preState.setInt(4, this.customer_id_ref);
			preState.setDate(5, (java.sql.Date) this.date_create);
			preState.setInt(6, this.id);
			obj.Result = (preState.executeUpdate() != 0) ? true : false;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return obj;
	}

	@Override
	public ReturnObj Delete() {
		String sql = "delete from dbo.orders_bill where id = ?";
		ReturnObj obj = new ReturnObj();
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			preState.setInt(1, this.id);
			obj.Result = preState.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return obj;
	}

	@Override
	public ReturnObj GetInformation() {
		ReturnObj obj = new ReturnObj();
		String sql = "select id, warehouse_id_ref, orders_bill_type_name_ref, account_warehouse_username_ref, customer_id_ref, date_create from dbo.orders_bill  where id = ?";
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			preState.setInt(1, this.id);
			ResultSet rs = preState.executeQuery();
			while (rs.next()) {
				Orders_bill a = new Orders_bill();
				a.id = rs.getInt("id");
				a.warehouse_id_ref = rs.getInt("warehouse_id_ref");
				a.orders_bill_type_name_ref = rs
						.getString("orders_bill_type_name_ref");
				a.account_warehouse_username_ref = rs
						.getString("account_warehouse_username_ref");
				a.customer_id_ref = rs.getInt("customer_id_ref");
				a.date_create = rs.getDate("date_create");
				obj.ObjResult = a;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return obj;
	}

}
